/**
 * @file
 * @auth jinguangguo
 * @date 2016/12/15
 */

import Vue from 'vue';

Vue.directive('digital', {
    deep: true,
    bind(el, binding, vnode) {
        let scope = vnode.context;
        // let vModel = el.getAttribute('v-model');
        let vModel = binding.expression;
        let arrs = vModel.split('.');
        let attr0 = arrs[0];
        let attr1 = arrs[1];

        if (vModel) {
            scope.$watch(vModel, (newVal, oldVal) => {
                if (newVal !== '') {
                    if (/^\d+$/.test(newVal) === false) {
                        if (attr1) {
                            scope[attr0][attr1] = oldVal;
                        } else {
                            scope[attr0] = oldVal;
                        }

                    }
                }
            }, {deep: true});
        }
    },
    update(el, binding, vnode) {

    }
});